<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Buffer Objects &mdash; Python v2.6.2 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.6.2',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v2.6.2 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v2.6.2 documentation" href="../index.html" />
    <link rel="up" title="Concrete Objects Layer" href="concrete.html" />
    <link rel="next" title="Tuple Objects" href="tuple.html" />
    <link rel="prev" title="Unicode Objects and Codecs" href="unicode.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="tuple.html" title="Tuple Objects"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="unicode.html" title="Unicode Objects and Codecs"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >Python/C API Reference Manual</a> &raquo;</li>
          <li><a href="concrete.html" accesskey="U">Concrete Objects Layer</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="buffer-objects">
<span id="bufferobjects"></span><h1>Buffer Objects<a class="headerlink" href="#buffer-objects" title="Permalink to this headline">¶</a></h1>
<p id="index-0">Python objects implemented in C can export a group of functions called the
&#8220;buffer interface.&#8221;  These functions can be used by an object to expose its data
in a raw, byte-oriented format. Clients of the object can use the buffer
interface to access the object data directly, without needing to copy it first.</p>
<p>Two examples of objects that support the buffer interface are strings and
arrays. The string object exposes the character contents in the buffer
interface&#8217;s byte-oriented form. An array can also expose its contents, but it
should be noted that array elements may be multi-byte values.</p>
<p>An example user of the buffer interface is the file object&#8217;s <tt class="xref docutils literal"><span class="pre">write()</span></tt>
method. Any object that can export a series of bytes through the buffer
interface can be written to a file. There are a number of format codes to
<a title="PyArg_ParseTuple" class="reference external" href="arg.html#PyArg_ParseTuple"><tt class="xref docutils literal"><span class="pre">PyArg_ParseTuple</span></tt></a> that operate against an object&#8217;s buffer interface,
returning data from the target object.</p>
<p id="index-1">More information on the buffer interface is provided in the section
<a class="reference external" href="typeobj.html#buffer-structs"><em>Buffer Object Structures</em></a>, under the description for <a title="PyBufferProcs" class="reference external" href="typeobj.html#PyBufferProcs"><tt class="xref docutils literal"><span class="pre">PyBufferProcs</span></tt></a>.</p>
<p>A &#8220;buffer object&#8221; is defined in the <tt class="docutils literal"><span class="pre">bufferobject.h</span></tt> header (included by
<tt class="docutils literal"><span class="pre">Python.h</span></tt>). These objects look very similar to string objects at the
Python programming level: they support slicing, indexing, concatenation, and
some other standard string operations. However, their data can come from one of
two sources: from a block of memory, or from another object which exports the
buffer interface.</p>
<p>Buffer objects are useful as a way to expose the data from another object&#8217;s
buffer interface to the Python programmer. They can also be used as a zero-copy
slicing mechanism. Using their ability to reference a block of memory, it is
possible to expose any data to the Python programmer quite easily. The memory
could be a large, constant array in a C extension, it could be a raw block of
memory for manipulation before passing to an operating system library, or it
could be used to pass around structured data in its native, in-memory format.</p>
<dl class="ctype">
<dt id="PyBufferObject">
<tt class="descname">PyBufferObject</tt><a class="headerlink" href="#PyBufferObject" title="Permalink to this definition">¶</a></dt>
<dd>This subtype of <a title="PyObject" class="reference external" href="structures.html#PyObject"><tt class="xref docutils literal"><span class="pre">PyObject</span></tt></a> represents a buffer object.</dd></dl>

<dl class="cvar">
<dt id="PyBuffer_Type">
<a title="PyTypeObject" class="reference external" href="type.html#PyTypeObject">PyTypeObject</a> <tt class="descname">PyBuffer_Type</tt><a class="headerlink" href="#PyBuffer_Type" title="Permalink to this definition">¶</a></dt>
<dd><p id="index-2">The instance of <a title="PyTypeObject" class="reference external" href="type.html#PyTypeObject"><tt class="xref docutils literal"><span class="pre">PyTypeObject</span></tt></a> which represents the Python buffer type;
it is the same object as <tt class="docutils literal"><span class="pre">buffer</span></tt> and  <tt class="docutils literal"><span class="pre">types.BufferType</span></tt> in the Python
layer. .</p>
</dd></dl>

<dl class="cvar">
<dt id="Py_END_OF_BUFFER">
int <tt class="descname">Py_END_OF_BUFFER</tt><a class="headerlink" href="#Py_END_OF_BUFFER" title="Permalink to this definition">¶</a></dt>
<dd>This constant may be passed as the <em>size</em> parameter to
<a title="PyBuffer_FromObject" class="reference internal" href="#PyBuffer_FromObject"><tt class="xref docutils literal"><span class="pre">PyBuffer_FromObject</span></tt></a> or <a title="PyBuffer_FromReadWriteObject" class="reference internal" href="#PyBuffer_FromReadWriteObject"><tt class="xref docutils literal"><span class="pre">PyBuffer_FromReadWriteObject</span></tt></a>.  It
indicates that the new <a title="PyBufferObject" class="reference internal" href="#PyBufferObject"><tt class="xref docutils literal"><span class="pre">PyBufferObject</span></tt></a> should refer to <em>base</em> object
from the specified <em>offset</em> to the end of its exported buffer.  Using this
enables the caller to avoid querying the <em>base</em> object for its length.</dd></dl>

<dl class="cfunction">
<dt id="PyBuffer_Check">
int <tt class="descname">PyBuffer_Check</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *p</em><big>)</big><a class="headerlink" href="#PyBuffer_Check" title="Permalink to this definition">¶</a></dt>
<dd>Return true if the argument has type <a title="PyBuffer_Type" class="reference internal" href="#PyBuffer_Type"><tt class="xref docutils literal"><span class="pre">PyBuffer_Type</span></tt></a>.</dd></dl>

<dl class="cfunction">
<dt id="PyBuffer_FromObject">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyBuffer_FromObject</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *base</em>, Py_ssize_t<em> offset</em>, Py_ssize_t<em> size</em><big>)</big><a class="headerlink" href="#PyBuffer_FromObject" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Return a new read-only buffer object.  This raises <a title="exceptions.TypeError" class="reference external" href="../library/exceptions.html#exceptions.TypeError"><tt class="xref docutils literal"><span class="pre">TypeError</span></tt></a> if <em>base</em>
doesn&#8217;t support the read-only buffer protocol or doesn&#8217;t provide exactly one
buffer segment, or it raises <a title="exceptions.ValueError" class="reference external" href="../library/exceptions.html#exceptions.ValueError"><tt class="xref docutils literal"><span class="pre">ValueError</span></tt></a> if <em>offset</em> is less than zero.
The buffer will hold a reference to the <em>base</em> object, and the buffer&#8217;s contents
will refer to the <em>base</em> object&#8217;s buffer interface, starting as position
<em>offset</em> and extending for <em>size</em> bytes. If <em>size</em> is <a title="Py_END_OF_BUFFER" class="reference internal" href="#Py_END_OF_BUFFER"><tt class="xref docutils literal"><span class="pre">Py_END_OF_BUFFER</span></tt></a>,
then the new buffer&#8217;s contents extend to the length of the <em>base</em> object&#8217;s
exported buffer data.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyBuffer_FromReadWriteObject">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyBuffer_FromReadWriteObject</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *base</em>, Py_ssize_t<em> offset</em>, Py_ssize_t<em> size</em><big>)</big><a class="headerlink" href="#PyBuffer_FromReadWriteObject" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Return a new writable buffer object.  Parameters and exceptions are similar to
those for <a title="PyBuffer_FromObject" class="reference internal" href="#PyBuffer_FromObject"><tt class="xref docutils literal"><span class="pre">PyBuffer_FromObject</span></tt></a>.  If the <em>base</em> object does not export
the writeable buffer protocol, then <a title="exceptions.TypeError" class="reference external" href="../library/exceptions.html#exceptions.TypeError"><tt class="xref docutils literal"><span class="pre">TypeError</span></tt></a> is raised.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyBuffer_FromMemory">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyBuffer_FromMemory</tt><big>(</big>void<em> *ptr</em>, Py_ssize_t<em> size</em><big>)</big><a class="headerlink" href="#PyBuffer_FromMemory" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Return a new read-only buffer object that reads from a specified location in
memory, with a specified size.  The caller is responsible for ensuring that the
memory buffer, passed in as <em>ptr</em>, is not deallocated while the returned buffer
object exists.  Raises <a title="exceptions.ValueError" class="reference external" href="../library/exceptions.html#exceptions.ValueError"><tt class="xref docutils literal"><span class="pre">ValueError</span></tt></a> if <em>size</em> is less than zero.  Note that
<a title="Py_END_OF_BUFFER" class="reference internal" href="#Py_END_OF_BUFFER"><tt class="xref docutils literal"><span class="pre">Py_END_OF_BUFFER</span></tt></a> may <em>not</em> be passed for the <em>size</em> parameter;
<a title="exceptions.ValueError" class="reference external" href="../library/exceptions.html#exceptions.ValueError"><tt class="xref docutils literal"><span class="pre">ValueError</span></tt></a> will be raised in that case.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyBuffer_FromReadWriteMemory">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyBuffer_FromReadWriteMemory</tt><big>(</big>void<em> *ptr</em>, Py_ssize_t<em> size</em><big>)</big><a class="headerlink" href="#PyBuffer_FromReadWriteMemory" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Similar to <a title="PyBuffer_FromMemory" class="reference internal" href="#PyBuffer_FromMemory"><tt class="xref docutils literal"><span class="pre">PyBuffer_FromMemory</span></tt></a>, but the returned buffer is writable.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyBuffer_New">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyBuffer_New</tt><big>(</big>Py_ssize_t<em> size</em><big>)</big><a class="headerlink" href="#PyBuffer_New" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Return a new writable buffer object that maintains its own memory buffer of
<em>size</em> bytes.  <a title="exceptions.ValueError" class="reference external" href="../library/exceptions.html#exceptions.ValueError"><tt class="xref docutils literal"><span class="pre">ValueError</span></tt></a> is returned if <em>size</em> is not zero or positive.
Note that the memory buffer (as returned by <a title="PyObject_AsWriteBuffer" class="reference external" href="objbuffer.html#PyObject_AsWriteBuffer"><tt class="xref docutils literal"><span class="pre">PyObject_AsWriteBuffer</span></tt></a>) is
not specifically aligned.</p>
</dd></dl>

</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h4>Previous topic</h4>
            <p class="topless"><a href="unicode.html"
                                  title="previous chapter">Unicode Objects and Codecs</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="tuple.html"
                                  title="next chapter">Tuple Objects</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/c-api/buffer.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="tuple.html" title="Tuple Objects"
             >next</a> |</li>
        <li class="right" >
          <a href="unicode.html" title="Unicode Objects and Codecs"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >Python/C API Reference Manual</a> &raquo;</li>
          <li><a href="concrete.html" >Concrete Objects Layer</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; <a href="../copyright.html">Copyright</a> 1990-2009, Python Software Foundation.
      Last updated on Apr 15, 2009.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.1.
    </div>
  </body>
</html>